﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Pager.Entity;

using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class  FrmTeamBookRoomManager : BaseForm
    {
        public FrmTeamBookRoomManager()
        {
            InitializeComponent();
        }

        private void FrmBookRoomManager_Load(object sender, EventArgs e)
        {

            txtBeginTime.Value = DateTime.Now.Date;
            txtEndTime.Value = DateTime.Now.Date;
            cmTimeType.SelectedIndex = 0;
            this.winGridViewPager1.dataGridView1.Columns.Add("status", "状态");
            this.winGridViewPager1.DisplayColumns = "RoomType,RoomCount,CustomerName,BookPeople,BookPeoplePhone,PreArrivalTime,PreOffTime,CustomerCompany,status";
            this.winGridViewPager1.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            //this.winGridViewPager1.dataGridView1.AutoGenerateColumns = false;
           // this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
           
           // this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
            BindData();
           // this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);

            this.winGridViewPager1.dataGridView1.Columns["Note"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["RoomNo"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["IsTeam"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["Account"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["BookNo"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["Source"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["CustomerTel"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["ID"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["IsFixRoomNo"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["RetentionTime"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["BookPeople"].Visible = false;
            this.winGridViewPager1.dataGridView1.Columns["BookPeoplePhone"].Visible = false;

            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
            //BindData();
            //this.winGridViewPager1.dataGridView1.Refresh();
           // winGridViewPager1.auto
        }

        void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in winGridViewPager1.dataGridView1.Rows)
            {

                if (row.Cells["BookStatus"].Value.ToString() == "0")
                {

                    row.Cells["status"].Value = "正常";


                }
                else if (row.Cells["BookStatus"].Value.ToString() == "1")
                {

                    row.Cells["status"].Value = "取消";
                }
                else
                {

                    row.Cells["status"].Value = "入住";

                }





            }

        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                  
                    BLLFactory<Book>.Instance.DelTeamBook(row.Cells["BookNo"].Value.ToString());

                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    string bookId = row.Cells["ID"].Value.ToString();
                    BookInfo bookInfo = BLLFactory<Book>.Instance.FindByID(bookId);
                    if (bookInfo != null)
                    {
                        FrmEditTeamBookRoom dlg = new FrmEditTeamBookRoom();
                        dlg.bookInfo = bookInfo;
                        if (DialogResult.OK == dlg.ShowDialog())
                        {
                            BindData();
                        }
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            FrmBookRoom dlg = new FrmBookRoom();
            dlg.ShowDialog();
            BindData();
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<Book>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }


        private void SetColumnAlias()
        {



            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("RoomType", "房间类型");
            this.winGridViewPager1.AddColumnAlias("RoomCount", "房间数");
            this.winGridViewPager1.AddColumnAlias("CustomerName", "团队名称");
            this.winGridViewPager1.AddColumnAlias("BookPeople", "预定人");
            this.winGridViewPager1.AddColumnAlias("BookPeoplePhone", "联系电话");
            this.winGridViewPager1.AddColumnAlias("PreArrivalTime", "预到达时间");
            this.winGridViewPager1.AddColumnAlias("PreOffTime", "预离开时间");
            this.winGridViewPager1.AddColumnAlias("CustomerCompany", "公司名称");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            this.winGridViewPager1.AddColumnAlias("IsCancel", "是否已经取消");
            #endregion
        
        
        
        }


        private void BindData()
        {

            SetColumnAlias();
            SearchCondition condition = new SearchCondition();
            condition.AddCondition("CustomerName", this.txtName.Text, SqlOperator.Like);
                //.AddCondition("CustomerName", this.txtName.Text, SqlOperator.Like)
                //.AddCondition("CustomerTel", this.txtName.Text, SqlOperator.Like);
            if (cmTimeType.Text == "预抵时间")
                 {
                  condition.AddCondition("PreArrivalTime", txtBeginTime.Text, SqlOperator.MoreThanOrEqual);
                  condition.AddCondition("PreArrivalTime", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual);

                 }else

                 {
                    condition.AddCondition("PreOffTime", txtBeginTime.Text, SqlOperator.MoreThanOrEqual);
                    condition.AddCondition("PreOffTime", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual);

                 }

            condition.AddCondition("IsTeam", 1, SqlOperator.Equal);
            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<BookInfo> list = BLLFactory<Book>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<BookInfo>(list);

            this.winGridViewPager1.dataGridView1.Refresh();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void menu_Cancel_Click(object sender, EventArgs e)
        {
            DataGridView dg = this.winGridViewPager1.dataGridView1;
            if (dg.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dg.SelectedRows)
                {
                    if (row.Cells["BookNo"].Value != null)
                    {
                        string  bookNo = (row.Cells["BookNo"].Value.ToString());
                        int status = Convert.ToInt32(row.Cells["BookStatus"].Value);
                        if (status == 1 || status == 2)
                        {
                            return;
                        }
                        BLLFactory<Book>.Instance.UpdateTeamBook(bookNo, 1);
                       // BLLFactory<Room>.Instance.SetRoomStatus(row.Cells["RoomNo"].Value.ToString(), RoomStatus.空净房.ToString());
                        MessageUtil.ShowTips("预定取消成功！");
                        BindData();
                    }
                    break;
                }
            }            
        }

        private void menu_Order_Click(object sender, EventArgs e)
        {
            DataGridView dg = this.winGridViewPager1.dataGridView1;
            if (dg.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dg.SelectedRows)
                {
                    if (row.Cells["ID"].Value != null)
                    {
                        int status = Convert.ToInt32(row.Cells["BookStatus"].Value);
                        if (status == 1)
                        {

                            MessageUtil.ShowTips("预定信息已经取消了，不能进行入住操作");
                            return;

                        }
                        else if (status == 2)
                        {

                            MessageUtil.ShowTips("该团队已经入住了");
                            return;
                        
                        }

                        BookInfo bookInfo = BLLFactory<Book>.Instance.FindByID(Convert.ToInt32(row.Cells["ID"].Value.ToString()));
                        FrmEditRoomGroupOrder obj = new FrmEditRoomGroupOrder();
                        obj.bookInfo = bookInfo;
                        obj.ShowDialog();
                        BindData();
                      
                    }
                    break;
                }
            }          
        }

        private void btnSearchToday_Click(object sender, EventArgs e)
        {


            SetColumnAlias();

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("PreArrivalTime", System.DateTime.Now.ToString("yyyy-MM-dd"), SqlOperator.MoreThanOrEqual);
            condition.AddCondition("PreArrivalTime", System.DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:00", SqlOperator.LessThanOrEqual);
            condition.AddCondition("IsTeam", 1, SqlOperator.Equal);
            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<BookInfo> list = BLLFactory<Book>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<BookInfo>(list);

            this.winGridViewPager1.dataGridView1.Refresh();

        }

        private void BookRoomMenuItem_Click(object sender, EventArgs e)
        {
           
            FrmEditTeamBookRoom dlg = new FrmEditTeamBookRoom();
            dlg.ShowDialog();
            BindData();
        }
    }
}
